#include "ColladaMaterialData.h"

namespace lib3dw
{
	ColladaMaterialData::ColladaMaterialData(FCDocument* colladaDocument)
	{
		diffuseMaterial = new GLfloat[4];
		diffuseMaterial[0] = 1.0;
		diffuseMaterial[1] = 1.0;
		diffuseMaterial[2] = 1.0;
		diffuseMaterial[3] = 1.0;

		ambientMaterial = new GLfloat[4];
		ambientMaterial[0] = 0.0;
		ambientMaterial[1] = 0.0;
		ambientMaterial[2] = 0.0;
		ambientMaterial[3] = 1.0;

		specularMaterial = new GLfloat[4];
		specularMaterial[0] = 1.0;
		specularMaterial[1] = 1.0;
		specularMaterial[2] = 1.0;
		specularMaterial[3] = 1.0;

		emissionMaterial = new GLfloat[4];
		emissionMaterial[0] = 0.0;
		emissionMaterial[1] = 0.0;
		emissionMaterial[2] = 0.0;
		emissionMaterial[3] = 1.0;

		shininess = 0.0;
		
		FCDMaterialLibrary *library = colladaDocument->GetMaterialLibrary();
		ProcessMaterialLibrary(library);
	}

	ColladaMaterialData::~ColladaMaterialData()
	{
		delete[] diffuseMaterial;
		delete[] ambientMaterial;
		delete[] specularMaterial;
		delete[] emissionMaterial;
	}

	void ColladaMaterialData::ProcessMaterialLibrary(FCDMaterialLibrary* library)
	{
		size_t materialCount = library->GetEntityCount();

		for (size_t i=0; i<materialCount;i++)
		{
			FCDMaterial* material = library->GetEntity(i);
			FCDEffect* effect = material->GetEffect();

			size_t profileCount = effect->GetProfileCount();
			for (size_t j=0;j<profileCount;j++)
			{
				FCDEffectProfile* profile = effect->GetProfile(j);
				FUDaeProfileType::Type type = profile->GetType();
				if (type == FUDaeProfileType::COMMON )
				{
					FCDEffectStandard* standardProfile = dynamic_cast<FCDEffectStandard*>(profile);
					diffuseMaterial[0] = standardProfile->GetDiffuseColor().x;
					diffuseMaterial[1] = standardProfile->GetDiffuseColor().y;
					diffuseMaterial[2] = standardProfile->GetDiffuseColor().z;
					diffuseMaterial[3] = standardProfile->GetDiffuseColor().w;
					
					ambientMaterial[0] = standardProfile->GetAmbientColor().x;
					ambientMaterial[1] = standardProfile->GetAmbientColor().y;
					ambientMaterial[2] = standardProfile->GetAmbientColor().z;
					ambientMaterial[3] = standardProfile->GetAmbientColor().w;
					
					specularMaterial[0] = standardProfile->GetSpecularColor().x;
					specularMaterial[1] = standardProfile->GetSpecularColor().y;
					specularMaterial[2] = standardProfile->GetSpecularColor().z;
					specularMaterial[3] = standardProfile->GetSpecularColor().w;
					
					emissionMaterial[0] = standardProfile->GetEmissionColor().x;
					emissionMaterial[1] = standardProfile->GetEmissionColor().y;
					emissionMaterial[2] = standardProfile->GetEmissionColor().z;
					emissionMaterial[3] = standardProfile->GetEmissionColor().w;
					
					shininess = standardProfile->GetShininess();
				}
			}
		}
	}
}